1 // Fig. 7.9: employ1.h 2 // An employee class 3 #ifndef EMPLOY1_H 4 #define EMPLOY1_H 5 6 class Employee { 7 public: 8 Employee( const char*, const char* ); // constructor 9 ~Employee(); // destructor 10 const char *getFirstName() const; // return first name 11 const char *getLastName() const; // return last name 12 13 // static member function 14 static int getCount(); // return # objects instantiated 15 16 private: 17 char *firstName; 18 char *lastName; 19 20 // static data member 21 static int count; // number of objects instantiated 22 }; 23 24 #endif 25 // Fig. 7.9: employ1.cpp 26 // Member function definitions for class Employee 27 #include 28 #include 29 #include 30 #include "employ1.h" 31 32 // Initialize the static data member 33 int Employee::count = 0; 34 35 // Define the static member function that 36 // returns the number of employee objects instantiated. 37 int Employee::getCount() { return count; } 38 39 // Constructor dynamically allocates space for the 40 // first and last name and uses strcpy to copy 41 // the first and last names into the object 42 Employee::Employee( const char *first, const char *last ) 43 { 44 firstName = new char[ strlen( first ) + 1 ]; 45 assert( firstName != 0 ); // ensure memory allocated 46 strcpy( firstName, first ); 47 48 lastName = new char[ strlen( last ) + 1 ]; 49 assert( lastName != 0 ); // ensure memory allocated 50 strcpy( lastName, last ); 51 52 ++count; // increment static count of employees 53 cout << "Employee constructor for " << firstName 54 << ' ' << lastName << " called." << endl; 55 } 56 57 // Destructor deallocates dynamically allocated memory 58 Employee::~Employee() 59 { 60 cout << "~Employee() called for " << firstName 61 << ' ' << lastName << endl; 62 delete firstName; // recapture memory 63 delete lastName; // recapture memory 64 --count; // decrement static count of employees 65 } 66 67 // Return first name of employee 68 const char *Employee::getFirstName() const 69 { 70 // const before return type prevents client modifying 71 // private data. Client should copy returned string before 72 // destructor deletes storage to prevent undefined pointer. 73 return firstName; 74 } 75 76 // Return last name of employee 77 const char *Employee::getLastName() const 78 { 79 // const before return type prevents client modifying 80 // private data. Client should copy returned string before 81 // destructor deletes storage to prevent undefined pointer. 82 return lastName; 83 } 84 // Fig. 7.9: fig07_09.cpp 85 // Driver to test the Employee class 86 #include 87 #include "employ1.h" 88 89 int main() 90 { 91 cout << "Number of employees before instantiation is " 92 << Employee::getCount() << endl; // use class name 93 94 Employee *e1Ptr = new Employee( "Susan", "Baker" ); 95 Employee *e2Ptr = new Employee( "Robert", "Jones" ); 96 97 cout << "Number of employees after instantiation is " 98 << e1Ptr->getCount(); 99 100 cout << "\n\nEmployee 1: " 101 << e1Ptr->getFirstName() 102 << " " << e1Ptr->getLastName() 103 << "\nEmployee 2: " 104 << e2Ptr->getFirstName() 105 << " " << e2Ptr->getLastName() << "\n\n"; 106 107 delete e1Ptr; // recapture memory 108 e1Ptr = 0; 109 delete e2Ptr; // recapture memory 110 e2Ptr = 0; 111 112 cout << "Number of employees after deletion is " 113 << Employee::getCount() << endl; 114 115 return 0; 116 }